home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / atre27.exe / ATREE_27 / OCRDEMO2 / OCR_RUN.CPP < prev    next >
C/C++ Source or Header  |  1992-08-01  |  16KB  |  613 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** ocr_run.cpp                                                         ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) M. Thomas, N. Sanche, W.W. Armstrong 1991, 1992       ****
  8.  ****                                                                                       ****
  9.  **** License:                                                            ****
  10.  **** A royalty-free license is granted for the use of this software for  ****
  11.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  12.  **** modified provided this notice appears in its entirety and unchanged ****
  13.  **** in all derived source programs.  Persons modifying the code are     ****
  14.  **** requested to state the date, the changes made and who made them     ****
  15.  **** in the modification history.                                        ****
  16.  ****                                                                     ****
  17.  **** Patent License:                                                     ****
  18.  **** The use of a digital circuit which transmits a signal indicating    ****
  19.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  20.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  21.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  22.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  23.  **** adapt logic trees using this program and its modifications.         ****
  24.  ****                                                                     ****
  25.  **** Limited Warranty:                                                   ****
  26.  **** This software is provided "as is" without warranty of any kind,     ****
  27.  **** either expressed or implied, including, but not limited to, the     ****
  28.  **** implied warrantees of merchantability and fitness for a particular  ****
  29.  **** purpose.  The entire risk as to the quality and performance of the  ****
  30.  **** program is with the user.  Neither the authors, nor the             ****
  31.  **** University of Alberta, its officers, agents, servants or employees  ****
  32.  **** shall be liable or responsible in any way for any damage to         ****
  33.  **** property or direct personal or consequential injury of any nature   ****
  34.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  35.  **** or any other party as a consequence of the use or disposition of    ****
  36.  **** this software.                                                      ****
  37.  **** Modification history:                                               ****
  38.  ****                                                                     ****
  39.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  40.  **** 92.03.07 Release 2.6, Monroe Thomas, Neal Sanche                    ****
  41.  **** 92.01.08 Release 2.7, Monroe Thomas, Neal Sanche                    ****
  42.  ****                                                                     ****
  43.  *****************************************************************************/
  44.  
  45. // ocr_run.cpp
  46.  
  47. #include "ocr.h"
  48.  
  49. BOOL
  50. TOcr::vote(LPFAST_TREE ftree[], LPBIT_VEC vec, int voters)
  51. {
  52.     int result = 0;
  53.     for(WORD i = 0; i < voters; i ++)
  54.     {
  55.         result += atree_fast_eval(ftree[i], vec);
  56.     }
  57.     return(result > (voters / 2));
  58. }
  59.  
  60. void
  61. TOcr::clear_bv(bit_vec bv)
  62. {
  63.     for(WORD i = 0; i < bv.len; i++)
  64.     {
  65.         bv_set(i, &(bv), 0);
  66.     }
  67. }
  68.  
  69. void
  70. TOcr::write_default()
  71. {
  72.     HANDLE ResHandle;
  73.     FILE* outstream;
  74.     FILE* instream;
  75.     int treedata;
  76.     int voters;
  77.  
  78.     ResHandle = FindResource(GetApplication()->hInstance,"def_aln","ALN");
  79.     treedata = AccessResource(GetApplication()->hInstance, ResHandle);
  80.  
  81.     if ((instream = fdopen(treedata, "r")) == NULL)
  82.     {
  83.         BWCCMessageBox(HWindow, "Can't open internal ALN resource", "OCR write_default()",
  84.                      MB_ICONEXCLAMATION | MB_OK);
  85.         exit(0);
  86.     }
  87.  
  88.     if ((outstream = fopen("ocr.tre", "w")) == NULL)
  89.     {
  90.         BWCCMessageBox(HWindow, "Can't open 'ocr.tre'", "write_default()",
  91.                      MB_ICONEXCLAMATION | MB_OK);
  92.         exit(0);
  93.     }
  94.  
  95.     HCURSOR hcursor = LoadCursor(NULL, IDC_WAIT);
  96.     hcursor = SetCursor(hcursor);
  97.  
  98.     fscanf(instream, "%d", &voters);
  99.     fprintf(outstream, "%d\n", voters);
  100.  
  101.     voters *= 3;
  102.  
  103.     for (WORD i = 0; i < voters; i++)
  104.     {
  105.         LPATREE atree = atree_read(instream);
  106.         atree_write(outstream, atree);
  107.     }
  108.  
  109.     fclose(instream);
  110.     fclose(outstream);
  111.  
  112.     SetCursor(hcursor);
  113. }
  114.  
  115. #define NUMBITS 512
  116. #define STEPLEVEL 16
  117.  
  118. void
  119. TOcr::OcrReverse()
  120. {
  121.     typedef    struct tagMemBit
  122.     {
  123.         WORD bitno;
  124.         BOOL flipto;
  125.     } MemBit;
  126.  
  127.     LPATREE atree;
  128.     LPFAST_TREE* ftreeA;
  129.     LPFAST_TREE* ftreeL;
  130.     LPFAST_TREE* ftreeN;
  131.     int voters;
  132.     bit_vec test;
  133.     bit_vec flipped;        // keeps track of bits already flipped
  134.     WORD letternumber;
  135.     WORD width = LetterBitmap->GetBytes();
  136.     long correct = 0;
  137.     long incorrect = 0;
  138.     long total = 0;
  139.     FILE* instream;
  140.     char szBuffer[10];
  141.     HBITMAP bitmaps[3];
  142.  
  143.     MemBit far* MemBits;
  144.  
  145.     TBitmap TestLetter(hLightOn);
  146.  
  147.     EnableWindow(Start->HWindow, FALSE);
  148.     EnableWindow(Train->HWindow, FALSE);
  149.     EnableWindow(Stop->HWindow, TRUE);
  150.     EnableWindow(Reverse->HWindow, FALSE);
  151.     EnableWindow(Reset->HWindow, FALSE);
  152.     EnableWindow(Quit->HWindow, FALSE);
  153.     EnableWindow(Forget->HWindow, FALSE);
  154.     EnableWindow(EditA->HWindow, FALSE);
  155.     EnableWindow(EditL->HWindow, FALSE);
  156.     EnableWindow(EditN->HWindow, FALSE);
  157.     EnableMenuItem(hSysMenu, SC_CLOSE, MF_GRAYED);
  158.  
  159.     MemBits = (MemBit far*) farmalloc(NUMBITS * sizeof(MemBits));
  160.  
  161.     if ((instream = fopen("ocr.tre", "r")) == NULL)
  162.     {
  163.         write_default();
  164.         if ((instream = fopen("ocr.tre", "r")) == NULL)
  165.         {
  166.             BWCCMessageBox(HWindow, "Can't open 'ocr.tre'", "OCR Reverse",
  167.                              MB_ICONEXCLAMATION | MB_OK);
  168.             exit(0);
  169.         }
  170.     }
  171.  
  172.     HCURSOR hcursor = LoadCursor(NULL, IDC_WAIT);
  173.     hcursor = SetCursor(hcursor);
  174.  
  175.     fscanf(instream, "%d", &voters);
  176.  
  177.     ftreeA = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  178.     for (WORD i = 0; i < voters; i++)
  179.     {
  180.         atree = atree_read(instream);
  181.         ftreeA[i] = atree_compress(atree);
  182.         atree_free(atree);
  183.     }
  184.  
  185.     ftreeL = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  186.     for (i = 0; i < voters; i++)
  187.     {
  188.         atree = atree_read(instream);
  189.         ftreeL[i] = atree_compress(atree);
  190.         atree_free(atree);
  191.     }
  192.  
  193.     ftreeN = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  194.     for (i = 0; i < voters; i++)
  195.     {
  196.         atree = atree_read(instream);
  197.         ftreeN[i] = atree_compress(atree);
  198.         atree_free(atree);
  199.     }
  200.  
  201.     SetCursor(hcursor);
  202.  
  203.     fclose(instream);
  204.  
  205.     Correct->Clear();
  206.     Incorrect->Clear();
  207.  
  208.     LetterBitmap->Init(hLetterClear);
  209.     LetterBitmap->Refresh();
  210.     LightBulbA->Init(hLightOff);
  211.     LightBulbA->Refresh();
  212.     LightBulbL->Init(hLightOff);
  213.     LightBulbL->Refresh();
  214.     LightBulbN->Init(hLightOff);
  215.     LightBulbN->Refresh();
  216.  
  217.     test.len = width * 8;
  218.     test.bv = (LPSTR)farmalloc(width);
  219.     flipped.len = width * 8;
  220.     flipped.bv = (LPSTR)farmalloc(width);
  221.  
  222.     stop = FALSE;
  223.  
  224.     bitmaps[0] = LetterA->GetBitmap();
  225.     bitmaps[1] = LetterL->GetBitmap();
  226.     bitmaps[2] = LetterN->GetBitmap();
  227.  
  228.     while (!stop)
  229.     {
  230.         letternumber = RANDOM(4);
  231.         if (letternumber == 0) TestLetter.Init(bitmaps[0]);
  232.         else if (letternumber == 1) TestLetter.Init(bitmaps[1]);
  233.         else if (letternumber == 2) TestLetter.Init(bitmaps[2]);
  234.         else TestLetter.Init(hLetterClear);
  235.  
  236.         TestLetter.Rotate((random(11) - 5));
  237.         translate(TestLetter.GetBitmap(), random(3) - 1, random(3) - 1);
  238.  
  239.         TestLetter.GetBits(test.bv);
  240.  
  241.         clear_bv(flipped);
  242.  
  243.         for (int i = 0; i < NUMBITS; i++)
  244.         {
  245.             WORD tmp = random(width * 8);
  246.             Windows_Interrupt(100);
  247.             while (bv_extract(tmp, &(flipped)))
  248.             {
  249.                 tmp = random(width * 8);
  250.             }
  251.             bv_set(tmp, &(flipped), 1);
  252.             MemBits[i].bitno = tmp;
  253.             MemBits[i].flipto = bv_extract(MemBits[i].bitno, &(test));
  254.             if (MemBits[i].flipto)
  255.             {
  256.                 bv_set(MemBits[i].bitno, &(test), 0);
  257.             }
  258.             else
  259.             {
  260.                 bv_set(MemBits[i].bitno, &(test), 1);
  261.             }
  262.         }
  263.  
  264.         LetterBitmap->SetBits(test.bv, width);
  265.         LetterBitmap->Refresh();
  266.  
  267.         for(i = (NUMBITS - 1); i >= 0; i -= STEPLEVEL)
  268.         {
  269.             if (stop) break;
  270.             Windows_Interrupt(100);
  271.  
  272.             for (WORD j = 0; j < STEPLEVEL; j ++)
  273.             {
  274.                 bv_set(MemBits[i-j].bitno, &(test), MemBits[i-j].flipto);
  275.             }
  276.  
  277.             LetterBitmap->SetBits(test.bv, width);
  278.  
  279.             if (shutdown) break;
  280.  
  281.             LetterBitmap->Refresh();
  282.  
  283.             total++;
  284.  
  285.             WORD numtrees1 = 0;
  286.             WORD treenumber = 0;
  287.             WORD treemask = 0;
  288.  
  289.             if (vote(ftreeA, &test, voters) == 1)
  290.             {
  291.                 numtrees1++;
  292.                 treenumber = 0;
  293.                 treemask |= 1;
  294.                 LightBulbA->Init(hLightOn);
  295.                 LightBulbA->Refresh();
  296.             }
  297.  
  298.             if (vote(ftreeL, &test, voters) == 1)
  299.             {
  300.                 numtrees1++;
  301.                 treenumber = 1;
  302.                 treemask |= 2;
  303.                 LightBulbL->Init(hLightOn);
  304.                 LightBulbL->Refresh();
  305.             }
  306.  
  307.             if (vote(ftreeN, &test, voters) == 1)
  308.             {
  309.                 numtrees1++;
  310.                 treenumber = 2 ;
  311.                 treemask |= 4;
  312.                 LightBulbN->Init(hLightOn);
  313.                 LightBulbN->Refresh();
  314.             }
  315.  
  316.             if ((numtrees1 == 0) && (letternumber == 3))
  317.             {
  318.                 // blank letter
  319.                 correct++;
  320.             }
  321.             else if ((numtrees1 != 1) || (treenumber != letternumber))
  322.             {
  323.                 // mistake!
  324.                 incorrect++;
  325.             }
  326.             else
  327.             {
  328.                 correct++;
  329.             }
  330.  
  331.             sprintf(szBuffer, "%7.2f", 100.0 * (float)correct/total);
  332.             Correct->SetText(szBuffer);
  333.             sprintf(szBuffer, "%7.2f", 100.0 * (float)incorrect/total);
  334.             Incorrect->SetText(szBuffer);
  335.  
  336.             if (treemask & 1)
  337.             {
  338.                 LightBulbA->Init(hLightOff);
  339.                 LightBulbA->Refresh();
  340.             }
  341.             if (treemask & 2)
  342.             {
  343.                 LightBulbL->Init(hLightOff);
  344.                 LightBulbL->Refresh();
  345.             }
  346.             if (treemask & 4)
  347.             {
  348.                 LightBulbN->Init(hLightOff);
  349.                 LightBulbN->Refresh();
  350.             }
  351.         }
  352.     }
  353.  
  354.     farfree(MemBits);
  355.     farfree(test.bv);
  356.     farfree(flipped.bv);
  357.     for (i = 0; i < voters; i++)
  358.     {
  359.         farfree(ftreeA[i]);
  360.         farfree(ftreeL[i]);
  361.         farfree(ftreeN[i]);
  362.     }
  363.     farfree(ftreeA);
  364.     farfree(ftreeL);
  365.     farfree(ftreeN);
  366.  
  367.     EnableWindow(Start->HWindow, TRUE);
  368.     EnableWindow(Train->HWindow, TRUE);
  369.     EnableWindow(Stop->HWindow, FALSE);
  370.     EnableWindow(Reverse->HWindow, TRUE);
  371.     EnableWindow(Reset->HWindow, TRUE);
  372.     EnableWindow(Quit->HWindow, TRUE);
  373.     EnableWindow(Forget->HWindow, TRUE);
  374.     EnableWindow(EditA->HWindow, TRUE);
  375.     EnableWindow(EditL->HWindow, TRUE);
  376.     EnableWindow(EditN->HWindow, TRUE);
  377.     EnableMenuItem(hSysMenu, SC_CLOSE, MF_ENABLED);
  378. }
  379.  
  380. void
  381. TOcr::OcrStart()
  382. {
  383.     LPATREE atree;
  384.     LPFAST_TREE* ftreeA;
  385.     LPFAST_TREE* ftreeL;
  386.     LPFAST_TREE* ftreeN;
  387.     int voters;
  388.     bit_vec test;
  389.     bit_vec flipped;
  390.     long correct = 0;
  391.     long incorrect = 0;
  392.     long total;
  393.     short letternumber;
  394.     char szBuffer[10];
  395.     FILE* instream;
  396.     HBITMAP bitmaps[3];
  397.     WORD width = LetterBitmap->GetBytes();
  398.  
  399.     EnableWindow(Start->HWindow, FALSE);
  400.     EnableWindow(Train->HWindow, FALSE);
  401.     EnableWindow(Stop->HWindow, TRUE);
  402.     EnableWindow(Reverse->HWindow, FALSE);
  403.     EnableWindow(Reset->HWindow, FALSE);
  404.     EnableWindow(Quit->HWindow, FALSE);
  405.     EnableWindow(Forget->HWindow, FALSE);
  406.     EnableWindow(EditA->HWindow, FALSE);
  407.     EnableWindow(EditL->HWindow, FALSE);
  408.     EnableWindow(EditN->HWindow, FALSE);
  409.     EnableMenuItem(hSysMenu, SC_CLOSE, MF_GRAYED);
  410.  
  411.     stop = FALSE;
  412.  
  413.     if ((instream = fopen("ocr.tre", "r")) == NULL)
  414.     {
  415.         write_default();
  416.         if ((instream = fopen("ocr.tre", "r")) == NULL)
  417.         {
  418.             BWCCMessageBox(HWindow, "Can't open 'ocr.tre'", "OCR Start",
  419.                              MB_ICONEXCLAMATION | MB_OK);
  420.             exit(0);
  421.         }
  422.     }
  423.  
  424.     HCURSOR hcursor = LoadCursor(NULL, IDC_WAIT);
  425.     hcursor = SetCursor(hcursor);
  426.  
  427.     fscanf(instream, "%d", &voters);
  428.  
  429.     ftreeA = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  430.     for (WORD i = 0; i < voters; i++)
  431.     {
  432.         atree = atree_read(instream);
  433.         ftreeA[i] = atree_compress(atree);
  434.         atree_free(atree);
  435.     }
  436.  
  437.     ftreeL = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  438.     for (i = 0; i < voters; i++)
  439.     {
  440.         atree = atree_read(instream);
  441.         ftreeL[i] = atree_compress(atree);
  442.         atree_free(atree);
  443.     }
  444.  
  445.     ftreeN = (LPFAST_TREE*) farmalloc(voters * sizeof(LPFAST_TREE));
  446.     for (i = 0; i < voters; i++)
  447.     {
  448.         atree = atree_read(instream);
  449.         ftreeN[i] = atree_compress(atree);
  450.         atree_free(atree);
  451.     }
  452.     SetCursor(hcursor);
  453.  
  454.     fclose(instream);
  455.  
  456.     test.len = width * 8;
  457.     test.bv = (LPSTR)farmalloc(width);
  458.     flipped.len = width * 8;
  459.     flipped.bv = (LPSTR)farmalloc(width);
  460.  
  461.     correct = incorrect = total = 0;
  462.  
  463.     LetterBitmap->Init(hLetterClear);
  464.     LetterBitmap->Refresh();
  465.  
  466.     Correct->Clear();
  467.     Incorrect->Clear();
  468.  
  469.     LightBulbA->Init(hLightOff);
  470.     LightBulbA->Refresh();
  471.     LightBulbL->Init(hLightOff);
  472.     LightBulbL->Refresh();
  473.     LightBulbN->Init(hLightOff);
  474.     LightBulbN->Refresh();
  475.  
  476.     bitmaps[0] = LetterA->GetBitmap();
  477.     bitmaps[1] = LetterL->GetBitmap();
  478.     bitmaps[2] = LetterN->GetBitmap();
  479.  
  480.     while (!stop)
  481.     {
  482. #define FORWARDBITS 288
  483.  
  484.         clear_bv(flipped);
  485.  
  486.         letternumber = RANDOM(3);
  487.         LetterBitmap->Init(bitmaps[letternumber]);
  488.         LetterBitmap->Rotate((random(11) - 5));
  489.         translate(LetterBitmap->GetBitmap(), random(3) - 1, random(3) - 1);
  490.  
  491.         if (stop) break;
  492.         LetterBitmap->GetBits(test.bv);
  493.         for(WORD i = 0; i < (FORWARDBITS/16); i++)
  494.         {
  495.             Windows_Interrupt(100);
  496.  
  497.             if (stop) break;
  498.  
  499.             total++;
  500.  
  501.             WORD numtrees1 = 0;
  502.             WORD treenumber = 0;
  503.             WORD treemask = 0;
  504.  
  505.             if (vote(ftreeA, &test, voters) == 1)
  506.             {
  507.                 numtrees1++;
  508.                 treenumber = 0;
  509.                 treemask |= 1;
  510.                 LightBulbA->Init(hLightOn);
  511.                 LightBulbA->Refresh();
  512.             }
  513.  
  514.             if (vote(ftreeL, &test, voters) == 1)
  515.             {
  516.                 numtrees1++;
  517.                 treenumber = 1;
  518.                 treemask |= 2;
  519.                 LightBulbL->Init(hLightOn);
  520.                 LightBulbL->Refresh();
  521.             }
  522.  
  523.             if (vote(ftreeN, &test, voters) == 1)
  524.             {
  525.                 numtrees1++;
  526.                 treenumber = 2 ;
  527.                 treemask |= 4;
  528.                 LightBulbN->Init(hLightOn);
  529.                 LightBulbN->Refresh();
  530.             }
  531.  
  532.             if ((numtrees1 != 1) || (treenumber != letternumber))
  533.             {
  534.                 // mistake!
  535.                 incorrect++;
  536.             }
  537.             else
  538.             {
  539.                 correct++;
  540.             }
  541.  
  542.             sprintf(szBuffer, "%7.2f", 100.0 * (float)correct/total);
  543.             Correct->SetText(szBuffer);
  544.             sprintf(szBuffer, "%7.2f", 100.0 * (float)incorrect/total);
  545.             Incorrect->SetText(szBuffer);
  546.  
  547.             // Add noise
  548.             if (i < ((FORWARDBITS/16) - 1))
  549.             {
  550.                 for (WORD j = i * (FORWARDBITS/16);
  551.                          j < (i * (FORWARDBITS/16) + (FORWARDBITS/16)); j++)
  552.                 {
  553.                     WORD tmp = random(width * 8);
  554.                     while (bv_extract(tmp, &(flipped)))
  555.                     {
  556.                         tmp = random(width * 8);
  557.                     }
  558.                     bv_set(tmp, &(flipped), 1);
  559.                     if(bv_extract(tmp, &(test)))
  560.                     {
  561.                         bv_set(tmp, &(test), 0);
  562.                     }
  563.                     else
  564.                     {
  565.                         bv_set(tmp, &(test), 1);
  566.                     }
  567.                 }
  568.             }
  569.  
  570.             LetterBitmap->SetBits(test.bv, width);
  571.             LetterBitmap->Refresh();
  572.             if (treemask & 1)
  573.             {
  574.                 LightBulbA->Init(hLightOff);
  575.                 LightBulbA->Refresh();
  576.             }
  577.             if (treemask & 2)
  578.             {
  579.                 LightBulbL->Init(hLightOff);
  580.                 LightBulbL->Refresh();
  581.             }
  582.             if (treemask & 4)
  583.             {
  584.                 LightBulbN->Init(hLightOff);
  585.                 LightBulbN->Refresh();
  586.             }
  587.         }
  588.     }
  589.  
  590.     farfree(test.bv);
  591.     farfree(flipped.bv);
  592.     for (i = 0; i < voters; i++)
  593.     {
  594.         farfree(ftreeA[i]);
  595.         farfree(ftreeL[i]);
  596.         farfree(ftreeN[i]);
  597.     }
  598.     farfree(ftreeA);
  599.     farfree(ftreeL);
  600.     farfree(ftreeN);
  601.  
  602.     EnableWindow(Start->HWindow, TRUE);
  603.     EnableWindow(Train->HWindow, TRUE);
  604.     EnableWindow(Stop->HWindow, FALSE);
  605.     EnableWindow(Reverse->HWindow, TRUE);
  606.     EnableWindow(Reset->HWindow, TRUE);
  607.     EnableWindow(Quit->HWindow, TRUE);
  608.     EnableWindow(Forget->HWindow, TRUE);
  609.     EnableWindow(EditA->HWindow, TRUE);
  610.     EnableWindow(EditL->HWindow, TRUE);
  611.     EnableWindow(EditN->HWindow, TRUE);
  612.     EnableMenuItem(hSysMenu, SC_CLOSE, MF_ENABLED);
  613. }